﻿using Cofe.Core.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Service;
using Cofe.Core.EntryType;
using Cofe.Core.Logger;
using Cofe.Core.Utils;
using Cofe.Core.Implements;

namespace Cofe.Core.Property
{
    public class EntryTypeSecondaryPP : PropertyProviderBase<IPropertyHost>
    {
        #region Defines

        public static string VolumeId = "EntryTypes";

        #endregion

        #region Factory

        [Export(typeof(ICofeService))]
        [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFE)]
        public class Factory : ISecondaryPropertyProviderFactory
        {
            public IEnumerable<IPropertyProvider> GetSecoondaryPropertyProviders(IPropertyHost propertyHost)
            {
                if (!(propertyHost.SecondaryPropertyProviders.Any((p) => p is EntryTypeSecondaryPP))
                    && propertyHost.Behaviors.IsPropertySupported(CofeProperties.Name))
                    yield return new EntryTypeSecondaryPP(propertyHost);
            }

            private  IServiceLocater _serviceLocater = null;
            public IServiceLocater ServiceLocater
            {
                get { return _serviceLocater ?? CofeServices.ServiceLocater; }
                set { _serviceLocater = value; }
            }

        }
        #endregion


        #region Constructor

        internal EntryTypeSecondaryPP(IPropertyHost propertyHost)
            : base(propertyHost, Cofe.Core.PropertyDefinitions.CofeDefinitions, Cofe.Core.PropertyDefinitions.CofeEntryTypeDefinitions)
        {            
            registerProperties(CofeEntryTypeProperties.EntryTypeKey, () => CofeServices.EntryTypeInfoManager.GetEntryTypeKey(propertyHost));            
            //registerProperties(CofeEntryTypeProperties.EntryTypeInfo, () => _etim.GetEntryTypeInfoAsync(propertyHost).Result);
            _additionalSupportedProperties.Add(CofeEntryTypeProperties.EntryTypeInfo);     
        }



        public override async Task<PropertyPair[]> GetPropertyAsync(object property)
        {
            if (property.Equals(CofeEntryTypeProperties.EntryTypeInfo))
                return new PropertyPair[] { PropertyPair.FromValue(property, 
                    await CofeServices.EntryTypeInfoManager.GetEntryTypeInfoAsync(EntryTypes.VolumeKey, Entity)) };

            return await base.GetPropertyAsync(property);
        }

        #endregion
    }
}
